/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import bean.GasElpiji;
import bean.Pengiriman;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author YosephDewa
 */
@WebServlet(urlPatterns = {"/FilterCetakFaktur"})
public class FilterCetakFaktur extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Pengiriman pengiriman = new Pengiriman();
        GasElpiji gasElpiji = new GasElpiji();
        String kode_pemesanan = request.getParameter("kode_pemesanan");
        String jumlah = request.getParameter("jumlah");
        String harga = request.getParameter("harga");
        pengiriman.setKodePemesanan(kode_pemesanan);
        if ("".equals(jumlah) || "".equals(harga) || "[0-9]*".equals(harga) || "[0-9]*".equals(jumlah)
                || gasElpiji.bandingJumlahGasLPG(Double.parseDouble(jumlah), pengiriman.tampilJenisGasElpiji()) == false) {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("CetakFaktur.jsp");
            requestDispatcher.forward(request, response);
        } else {
            Cookie[] ck = request.getCookies();
            pengiriman.setUsername(ck[1].getValue());
            pengiriman.setJumlahGasElpiji(Double.parseDouble(jumlah));
            pengiriman.setHargaGasElpiji(Double.parseDouble(harga));
            Double total = Double.parseDouble(jumlah) * Double.parseDouble(harga);
            pengiriman.setStatus("belum");
            pengiriman.insertPengiriman();
            out.println("<head>"
                    + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
                    + "<title>Data Tersimpan</title>");
            out.println("<script type=\"text/javascript\" src=\"libs/base64.js\"></script>\n"
                    + "        <script type=\"text/javascript\" src=\"libs/sprintf.js\"></script>\n"
                    + "        <script type=\"text/javascript\" src=\"libs/jspdf.js\"></script>");
            out.println("<script type=\"text/javascript\">\n"
                    + "            function demo1() {\n"
                    + "                var doc = new jsPDF();\n"
                    + "\n"
                    + "                doc.text(75, 20, 'PT. KEBON ARUM GROUP');\n"
                    + "                doc.setFontSize(10);\n"
                    + "                doc.text(45, 30, 'Jalan Pahlawan no. 14, Kec. Temanggung, Kab. Temanggung, Jawa Tengah 4557.');\n"
                    + "                doc.setFontSize(12);\n"
                    + "                doc.text(10, 40, '________________________________________________________________________________');\n"
                    + "                doc.text(85, 50, 'FAKTUR PEMBELIAN');\n"
                    + "                \n"
                    + "                doc.setFontSize(10);\n"
                    + "                doc.text(20, 70, 'Kode Pengiriman : " + pengiriman.getKodePengiriman() + "');\n"
                    + "                doc.text(20, 75, 'Kode Pemesanan : " + pengiriman.getKodePemesanan() + "');\n"
                    + "                doc.text(20, 80, 'Nama Pengecer : " + pengiriman.tampilNamaPemesan() + "');  \n"
                    + "                doc.text(20, 85, 'Jenis Tabung Gas : " + pengiriman.tampilJenisGasElpiji() + "');\n"
                    + "                doc.text(20, 90, 'Jumlah : " + pengiriman.getJumlahGasElpiji() + "');\n"
                    + "                doc.text(20, 95, 'Total Harga : " + total + "');\n"
                    + "                \n"
                    + "                doc.text(20, 105, 'Jumlah Diterima : . . . ');\n"
                    + "                doc.text(20, 110, 'Total Harga Diterima : . . . . . ');\n"
                    + "                \n"
                    + "                doc.text(144, 130, '" + pengiriman.getWaktuPengiriman() + "');\n"
                    + "                doc.text(26, 140, 'Pengirim');\n"
                    + "                doc.text(20, 155, '(_____________)');\n"
                    + "                doc.text(145, 140, 'Penerima');\n"
                    + "                doc.text(139, 155, '(_____________)');\n"
                    + "\n"
                    + "                // Output as Data URI\n"
                    + "                doc.output('datauri');\n"
                    + "            }\n"
                    + "        </script>");
            out.println("</head>");
            out.println("<body bgcolor=\"#CCCCCC\">"
                    + "<table border=\"0\" align=\"center\"><tr>"
                    + "<td align=\"center\">"
                    + "<font style=\"font-size: 24px ; font-style: Arial ; color: background\" >FORM CETAK FAKTUR GAS LPG</font><br>"
                    + "<font style=\"font-size: 14px ; font-style: Arial ; color: background\" >PT. KEBON ARUM GROUP</font>"
                    + "<br><br><h1 style=\"font-size: x-large\">DATA TERSIMPAN</h1>"
                    + "</td></tr></tabl>");
            out.println("<table align=\"center\" border=\"0\">");
            out.println("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"Tampil Faktur Pengiriman\" value=\"Tampil Faktur Pengiriman\" width=\"250\" onclick=demo1() "
                    + "target=\"_blank\"/></td>"
                    + "</tr>");
            out.println("<form action=\"MenuUtamaAdmin.jsp\"><tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"kembali\" value=\"kembali ke form utama\" width=\"250\"/></td>"
                    + "</tr></form></table>");
            out.println("</body>");
            out.println("</html>");
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
